package com.pulaien.shop.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.AjaxResult;
import com.pulaien.common.core.page.TableDataInfo;
import com.pulaien.common.utils.ConstantsUtil;
import com.pulaien.common.utils.date.DateFormat;
import com.pulaien.common.utils.date.DateUtils;
import com.pulaien.common.utils.date.EnumDateStyle;
import com.pulaien.common.utils.poi.ExcelUtils;
import com.pulaien.system.domain.Container;
import com.pulaien.system.domain.Customer;
import com.pulaien.system.domain.dto.*;
import com.pulaien.system.service.IBoxOrderService;
import com.pulaien.system.service.IBoxService;
import com.pulaien.system.service.IContainerService;
import com.pulaien.system.service.ICustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 箱子订单关系Controller
 *
 * @author pulaien
 */
@RestController
@RequestMapping("/shop/boxOrder")
@Api(tags = "商城端客戶箱子订单关系")
public class ShopBoxOrderController extends BaseController
{
    @Resource
    private IBoxService boxService;
    @Resource
    private IContainerService containerService;


    @ApiOperation(value = "编辑箱子 实重、抛重、箱子类型")
    @RequestMapping(value = "/updateBox", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updateBox(@RequestBody ExtBox extBox){
        return this.boxService.updateBox(extBox);
    }

    @ApiOperation("查询箱子列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public TableDataInfo list(BoxQueryParam queryParam) {

        queryParam.setDelFlag(ConstantsUtil.FLG_NORMAL);
        // 下单时间
        if (StringUtils.isNotEmpty(queryParam.getCreateTimeStart())) {
            queryParam.setCreateTimeStart(String.valueOf(DateUtils.stringToLong(DateFormat.getDayStart(queryParam.getCreateTimeStart()), EnumDateStyle.YYYY_MM_DD_HH_MM_SS.getValue())));
        }
        if (StringUtils.isNotEmpty(queryParam.getCreateTimeEnd())) {
            queryParam.setCreateTimeEnd(String.valueOf(DateUtils.stringToLong(DateFormat.getDayEnd(queryParam.getCreateTimeEnd()), EnumDateStyle.YYYY_MM_DD_HH_MM_SS.getValue())));
        }
        List<ExtBox> boxList = boxService.list(queryParam);
        return getDataTable(boxList);
    }

    @ApiOperation(value = "获取移箱批次列表")
    @RequestMapping(value = "/getMoveContainerList/{boxId}", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getMoveContainerList(@PathVariable Long boxId){
        return this.containerService.getMoveContainerList(boxId);
    }

    @ApiOperation(value = "获取箱子列表")
    @RequestMapping(value = "/getBoxList/{containerBatchNo}", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getBoxList(@PathVariable String containerBatchNo){
        return this.boxService.getBoxListByContainerBatchNo(containerBatchNo);
    }

    @ApiOperation(value = "移箱接口")
    @RequestMapping(value = "/moveBox", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult moveBox(@RequestBody ExtBox extBox){
        return this.boxService.moveBox(extBox);
    }

    @ApiOperation(value = "合箱接口")
    @RequestMapping(value = "/mergeBox", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult mergeBox(@RequestBody ExtBox extBox){
        return this.boxService.mergeBox(extBox);
    }

    @ApiOperation("查询箱子订单列表")
    @RequestMapping(value = "/boxOrderlist", method = RequestMethod.GET)
    @ResponseBody
    public TableDataInfo boxOrderlist(BoxOrderQueryParam queryParam) {
        queryParam.setDelFlag(ConstantsUtil.FLG_NORMAL);
        // 下单时间
        if (StringUtils.isNotEmpty(queryParam.getCreateTimeStart())) {
            queryParam.setCreateTimeStart(String.valueOf(DateUtils.stringToLong(DateFormat.getDayStart(queryParam.getCreateTimeStart()), EnumDateStyle.YYYY_MM_DD_HH_MM_SS.getValue())));
        }
        if (StringUtils.isNotEmpty(queryParam.getCreateTimeEnd())) {
            queryParam.setCreateTimeEnd(String.valueOf(DateUtils.stringToLong(DateFormat.getDayEnd(queryParam.getCreateTimeEnd()), EnumDateStyle.YYYY_MM_DD_HH_MM_SS.getValue())));
        }
        List<ExtBoxOrder> boxList = boxService.boxOrderList(queryParam);
        return getDataTable(boxList);
    }

    @ApiOperation(value = "获取箱子详情")
    @RequestMapping(value = "/getBoxInfo/{boxNo}", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getBoxInfo(@PathVariable String boxNo) {
        return boxService.getBoxInfo(boxNo);
    }

}
